package com.example.demo.customer;


import com.example.demo.exception.ApiRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@RequestMapping(path = "api/v2/customers")       //为控制层Controller指定一个路径
@RestController
//构造器，创建此类
public class CustomerControllerV2 {

    private final CustomerService customerService;

    //新建依赖实例
    @Autowired
    private CustomerControllerV2(CustomerService customerService){
        this.customerService = customerService;
    }

    @GetMapping
    List<Customer> getCustomers() {return customerService.getCustomers();}

    @GetMapping(path = "{customerId}")
    Customer getCustomer(@PathVariable("customerId") Long id){
              return customerService.getCustomer(id);
    }

//    新增getCustomerException()
    @GetMapping(path = "{customerId}/exception")
//    path路径要打对！
    Customer getCustomerException(@PathVariable("customerId") Long id){
        throw new ApiRequestException(
                "ApiRequestException for customer " + id
        );
    }

    @PostMapping
    void createNewCustomer(@Valid @RequestBody Customer customer){
        System.out.println("POST 请求...");
        System.out.println(customer);
    }

    @PutMapping
    void updateCustomer(@RequestBody Customer customer){
        System.out.println("UPDATE 请求");
        System.out.println(customer);
    }


    @DeleteMapping(path = "{customerId}")        // （path = ""）定义了DeleteMapping方法的路径
    void deleteCustomer(@PathVariable("customerId") long id){
        System.out.println("DELETE 请求，删除Customer，其ID为"+ id);
    }

}
